package com.util.DBpool;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;

public class RedisConnection {

    private String REDIS_IP = "127.0.0.1";
    private Integer REDIS_PORT = 6379;

    private String module;
    private String base;
    private String keyword;

    private Jedis conn = null;


    private JedisPool jedisPool = null;

    public RedisConnection(String module, String base, String keyword) {
        this.module = module;
        this.base = base;
        this.keyword = keyword;
    }

    private Jedis getConnection() {
        if (jedisPool == null) {
            jedisPool = new JedisPool(new GenericObjectPoolConfig(), REDIS_IP, REDIS_PORT, 100000);
        }
        this.conn = jedisPool.getResource();
        return conn;
    }

    public String search_query() {
        conn = getConnection();

        return conn.get(getQueryKey());
    }

    public void set_search_query(String value) {
        conn = getConnection();
        conn.set(getQueryKey(), value);
    }

    private String getQueryKey() {
        return module + "_" + base + "_" + keyword;
    }

    public ArrayList<ArrayList<String>> jsonStr_to_table(String redis_search_result) {
        return null;

    }
}
